package servicos;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.nio.charset.Charset;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;


/**
 *
 * @author Edemilson Danker
 * @author Jefferson Sidney Rossetim
 * @author Rogerio Mello Vanti
 */

public class EncriptaBlowFish {

	
	public  String encryptBlowfish(String toEncode, String strkey) {
		
		Charset UTF8 = Charset.forName("UTF-8");

		String encrypted = null;
		if (strkey.getBytes(UTF8).length > 8) {
			strkey = strkey.substring(0, 8);
		}
		try {
			SecretKeySpec key = new SecretKeySpec(strkey.getBytes(UTF8), "Blowfish");
			Cipher cipher = Cipher.getInstance("Blowfish");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] encoded = cipher.doFinal(toEncode.getBytes(UTF8));
			encrypted = Base64.encode(encoded);
                        
                        
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encrypted;
	}
	
	
	public  String decryptBlowfish(String toDecode, String strkey) {
		Charset UTF8 = Charset.forName("UTF-8");

		String decryptedString = null;
		if (strkey.getBytes().length > 8) {
			strkey = strkey.substring(0, 8);
		}
		try {
			SecretKeySpec key = new SecretKeySpec(strkey.getBytes(UTF8), "Blowfish");
			Cipher cipher = Cipher.getInstance("Blowfish");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] decrypted = cipher.doFinal(Base64.decode(toDecode));
			decryptedString = new String(decrypted);
		} catch (Exception e){
			e.printStackTrace();
		}
		return decryptedString;
	}
        
}

